﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CRM_SharePoint_Transports
{
    public partial class SharePointListTransportConfigControl : UserControl, BlueIntegrator.Extensibility.IBlueIntegratorPropertyPage
    {
        public SharePointListTransportConfigControl(bool bWriteMode,  BlueIntegrator.Messaging.ReceivePortConfigurationForm rpcf, BlueIntegrator.Messaging.SendPortConfigurationForm spcf)
        {
            InitializeComponent();
            m_bWriteMode = bWriteMode;
            m_spcf = spcf;
            m_rpcf = rpcf;

            this.Text = "SharePoint";
            this.delCheckBox.Visible = !bWriteMode;
        }

        /// <summary>
        /// Configure click handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            SSISLib.SharePointListConfigForm scf = new SSISLib.SharePointListConfigForm(m_data, m_bWriteMode, m_portId, m_dal, (m_rpcf != null ? m_rpcf.GetTokens() : m_spcf != null ? m_spcf.GetTokens() : new string[0]));

            if (scf.ShowDialog(this) == DialogResult.OK)
            {
            }
        }

        #region IBlueIntegratorPropertyPage Members

        public void Initialise(System.Collections.IDictionary data)
        {
            m_data = new System.Collections.Hashtable();
            m_origData = new System.Collections.Hashtable();

            foreach (System.Collections.DictionaryEntry dent in data)
            {
                m_data[dent.Key] = dent.Value;
                m_origData[dent.Key] = dent.Value;
            }
            
            BlueIntegrator.DAL.DALBase dal = data["__DAL"] as BlueIntegrator.DAL.DALBase;
            m_dal = dal;
            m_portId = data["__PortId"] as string;
            this.delCheckBox.Checked = !BlueIntegrator.Helpers.Conversion.ConvertToBool(data["KeepAfterProcessed"]);
        }

        public bool ApplyChanges(System.Collections.IDictionary data)
        {
            if (data != m_data)
            foreach (System.Collections.DictionaryEntry dent in m_data)
            {
                if (dent.Value != m_origData[dent.Key] || !data.Contains(dent.Key))
                    data[dent.Key] = dent.Value;
            }

            data["KeepAfterProcessed"] = !this.delCheckBox.Checked;
            return true;
        }

        #endregion

        System.Collections.Hashtable m_data, m_origData;
        bool m_bWriteMode;
        string m_portId;
        BlueIntegrator.DAL.DALBase m_dal;
        BlueIntegrator.Messaging.SendPortConfigurationForm m_spcf;
        BlueIntegrator.Messaging.ReceivePortConfigurationForm m_rpcf;
    }
}
